#!/usr/bin/env bash
# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

MY_DIR=$(readlink -f $(dirname $0))

function usage() {
	cat <<EOF
usage:
./$0 [options]

options:
	-h|--help		    help
	-v|--verbose		verbose
	-a|--build-arch		cross compile arch
	-l|--llvm-path		llvm home
	-s|--sysroot 	    cross compile sysroot
	-d|--output-dir 	output dir
	-t|--output-stamp 	output stamp file
    -m|--defines        clang macro defines
EOF
}

if [[ "$#" -lt 5 ]]; then
	usage $0
	exit 1
fi

OPTIONS=help,verbose,build-arch:,llvm-path:,sysroot:,output-dir:,output-stamp:,defines:
SHORT_OPTIONS=hva:l:s:d:t:m:

SHORT_OPTIONS=$(getopt --options "$SHORT_OPTIONS" --longoptions "$OPTIONS" -- "$@")

if [ $? -ne 0 ]; then
	echo "Failed to parse options." >&2
	exit 1
fi

eval set -- "$SHORT_OPTIONS"

while [ -n "$1" ]; do
	case "$1" in
	-h | --help)
		usage $0
		shift
		exit 0
		;;
	-v | --verbose)
		VERBOSE=1
		shift
		;;
	-a | --build-arch)
		ARCH=$2
		shift 2
		;;
	-l | --llvm-path)
		LLVM=$(realpath $2)
		shift 2
		;;
	-s | --sysroot)
		SYSROOT_DIR=$(realpath $2)
		shift 2
		;;
	-d | --output-dir)
		OUTPUT=$(realpath $2)
		shift 2
		;;
	-t | --output-stamp)
		STAMP=$(realpath $2)
		shift 2
		;;
	-m | --defines)
		DEFS="$2"
		shift 2
		;;
	--)
		shift
		break
		;;
	*)
		echo "Unhandled option: $1" >&2
		exit 1
		;;
	esac
done

if [ $# -gt 0 ]; then
	echo "Trailing parameters: $@"
fi

DEFINES=
for i in $DEFS; do
	echo "$i"
	DEFINES="$DEFINES -D$i"
done

if [ -n "$VERBOSE" ]; then
	echo ARCH=$ARCH
	echo LLVM=$LLVM
	echo SYSROOT_DIR=$SYSROOT_DIR
	echo OUTPUT=$OUTPUT
	echo DEFINES=$DEFINES
	echo STAMP=$STAMP
fi

OPENSSL_PATH=$(realpath $MY_DIR/../openssl)
if [ ! -e "$OPENSSL_PATH" ]; then
	echo "OpenSSL source path not found: $OPENSSL_PATH" >&2
	exit 1
fi

OPENSSL_OBJ_PATH=$(realpath $SYSROOT_DIR/../openssl)
if [ ! -e "$OPENSSL_OBJ_PATH" ]; then
	echo "OpenSSL object path not found: $OPENSSL_OBJ_PATH" >&2
	exit 1
fi

OPENSSL_OBJ_INC_PATH=$(find ${OPENSSL_OBJ_PATH}/build_all_generated -maxdepth 2 -name "include" -type d)
if [ ! -e "$OPENSSL_OBJ_INC_PATH" ]; then
	echo "OpenSSL object include path not found: $OPENSSL_OBJ_INC_PATH" >&2
	exit 1
fi

####################################################################################################
export AS=${LLVM}/bin/llvm-as
export CC="${LLVM}/bin/clang --target=${ARCH}-linux-ohos --sysroot=${SYSROOT_DIR}"
export CXX="${LLVM}/bin/clang++ --target=${ARCH}-linux-ohos --sysroot=${SYSROOT_DIR}"
export LD="${LLVM}/bin/ld.lld"
export STRIP=${LLVM}/bin/llvm-strip
export RANLIB=${LLVM}/bin/llvm-ranlib
export OBJDUMP=${LLVM}/bin/llvm-objdump
export OBJCOPY=${LLVM}/bin/llvm-objcopy
export NM=${LLVM}/bin/llvm-nm
export AR=${LLVM}/bin/llvm-ar

export CFLAGS="-fPIC -Wno-unused-but-set-variable -Wno-atomic-alignment -O3 $DEFINES"
export CXXFLAGS="-fPIC -O3 $DEFINES"
export LDFLAGS="--rtlib=compiler-rt -fuse-ld=lld"

pushd ${MY_DIR}
./autogen.sh
./configure \
  --target=${ARCH}-linux-ohos \
  --host=${ARCH}-linux-ohos \
  --prefix=${OUTPUT} \
  --enable-openssl \
  --enable-pem \
  --enable-pubkey \
  --enable-kernel-libipsec \
  --enable-kernel-pfkey \
  --enable-aes \
  --enable-des \
  --enable-x509 \
  --enable-nonce \
  --enable-static \
  --enable-monolithic \
  --enable-libipsec \
  --enable-dhcp \
  --enable-af-alg \
  --disable-mysql \
  --disable-sqlite \
  --enable-ccm \
  --enable-ctr \
  --enable-gcm \
  --enable-pkcs11 \
  --enable-pkcs1 \
  --enable-ikev1 \
  --enable-ikev2 \
  --disable-ldap \
  --enable-eap-identity \
  --enable-eap-mschapv2 \
  --enable-farp \
  --enable-vici \
  --disable-shared \
  --disable-gmp \
  --with-sysroot=${OHOS_SYSROOT} \
  LDFLAGS="--rtlib=compiler-rt -fuse-ld=lld -L${OPENSSL_OBJ_PATH}" \
  LIBS="-lssl_static -lcrypto_static" \
  CPPFLAGS="-I${OPENSSL_OBJ_INC_PATH} -I${OPENSSL_PATH}/include"

make clean
make
if [ $? -ne 0 ]; then
	echo "Failed to build strongswan." >&2
	exit 1
fi
make install
find ${OUTPUT} -name ".gitkeep" -delete;
$STRIP ${OUTPUT}/bin/pki \
  ${OUTPUT}/libexec/ipsec/charon \
  ${OUTPUT}/libexec/ipsec/starter \
  ${OUTPUT}/libexec/ipsec/stroke \
  ${OUTPUT}/libexec/ipsec/xfrmi \
  ${OUTPUT}/sbin/swanctl
touch $STAMP
popd
